package cn.cameltec.kissabc.pad.games;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import com.baijiayun.live.ui.utils.DisplayUtils;

import cn.cameltec.kissabc.utils.L;

/**
 * @author 文庆
 * @date 2019/2/20.
 * @description
 */
@SuppressLint("AppCompatCustomView")
public class DragImageView extends ImageView {

    private int parentHeight;
    private int parentWidth;
    private int startX, startY;
    private int lastX, lastY;
    private int lastRawX, lastRawY;
    private boolean isMove;

    public DragImageView(Context context) {
        super(context);
    }

    public DragImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public DragImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int halfHeight = DragImageView.this.getHeight() / 2;

        int rawX = (int) event.getRawX();
        int rawY = (int) event.getRawY();


        if (lastRawY == 0) {
            lastRawX = (int) getX();
            lastRawY = (int) getY();
        }
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                isMove = false;
                startX = lastX = rawX;
                startY = lastY = rawY;
                ViewGroup parent;
                if (getParent() != null) {
                    parent = (ViewGroup) getParent();
                    parentHeight = parent.getHeight();
                    parentWidth = parent.getWidth();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                isMove = true;
                float x = getX() + (rawX - lastX);
                float y = getY() + (rawY - lastY);
                //检测是否到达边缘 左上右下
                x = x < 0 ? 0 : x > parentWidth - getWidth() ? parentWidth - getWidth() : x;
                //修改bug 到顶部的时候要显示出半个身子
                y = getY() < halfHeight ? halfHeight : getY() + getHeight() > parentHeight ? parentHeight - getHeight() : y;
                setX(x);
                setY(y);
                lastX = rawX;
                lastY = rawY;
                break;
            case MotionEvent.ACTION_UP:
                int dx = (int) event.getRawX() - startX;
                int dy = (int) event.getRawY() - startY;
                if (Math.abs(dx) < 3 && Math.abs(dy) < 3) {
                    performClick();//重点，确保setOnclickListener生效
                    break;
                }
//                if (rawX >= parentWidth / 2) {
//                    //靠右吸附
//                    animate().setInterpolator(new DecelerateInterpolator())
//                            .setDuration(500)
//                            .xBy(parentWidth - getWidth() - getX())
//                            .start();
//                } else {
//                    //靠左吸附
//                    ObjectAnimator oa = ObjectAnimator.ofFloat(this, "x", getX(), 0);
//                    oa.setInterpolator(new DecelerateInterpolator());
//                    oa.setDuration(500);
//                    oa.start();
//                }

                if (!isRight) {
                    resetLastXY();
                } else {
                    moveRight();
                }

//                if (rawY < getHeight()) {
//                    setY(halfHeight);
//                }
//                if (rawY > parentHeight - getHeight()) {
//                    setY(parentHeight - getHeight());
//                }
                break;
        }
        return !isMove;
    }


    boolean isRight = false;
    float rightX = 0;
    float rightY = 0;

    public void init(boolean isRight, float x, float y) {
        this.isRight = isRight;
        rightX = x;
        rightY = y;

    }

    /**
     * 回到上个位置
     */
    public void resetLastXY() {


        ObjectAnimator oa = ObjectAnimator.ofFloat(this, "x",
                getX(), lastRawX);
        oa.setInterpolator(new DecelerateInterpolator());
        oa.setDuration(500);
        oa.start();
        ObjectAnimator yOa = ObjectAnimator.ofFloat(this, "y",
                getY(), lastRawY);
        yOa.setInterpolator(new DecelerateInterpolator());
        yOa.setDuration(500);
        yOa.start();


    }

    public void moveRight() {
        ObjectAnimator oa = ObjectAnimator.ofFloat(this, "x",
                getX(), rightX+ DisplayUtils.dip2px(getContext(),250f));
        oa.setInterpolator(new DecelerateInterpolator());
        oa.setDuration(500);
        oa.start();
        ObjectAnimator yOa = ObjectAnimator.ofFloat(this, "y",
                getY(), rightY+ DisplayUtils.dip2px(getContext(),100f));
        yOa.setInterpolator(new DecelerateInterpolator());
        yOa.setDuration(500);
        yOa.start();
    }

}